package ecom.servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.transaction.UserTransaction;

import ecom.beans.dish.DishEntity;
import ecom.beans.dish.DishEntityFacadeRemote;

public class DishlistServlet extends HttpServlet {
	/**
     * Serializable class uid.
     */
    private static final long serialVersionUID = 6893863749912962928L;
    private Context initialContext;
    private UserTransaction utx;
    private DishEntityFacadeRemote dishEntityFacade;


    /**
     * Called by the server (via the service method) to allow a servlet to
     * handle a GET request.
     * @param request an HttpServletRequest object that contains the request
     * the client has made of the servlet
     * @param response an HttpServletResponse object that contains the
     * response the servlet sends to the client
     * @throws IOException if an input or output error is detected when the
     * servlet handles the GET request
     * @throws ServletException if the request for the GET could not be handled
     */
    public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException {
    	
    	 initialContext = null;
         utx = null;
         dishEntityFacade = null;
        
    	response.setContentType("text/html");
        PrintWriter out = response.getWriter();
    	
    	System.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.objectweb.carol.jndi.spi.MultiOrbInitialContextFactory");
        
        try
        {
       	 initialContext = new InitialContext();
       	 //out.println("Creation of initialContext...OK   ");
        } 
        catch (Exception e)
        {
           System.err.println("Cannot get initial context for JNDI: " + e);
           System.exit(2);
        }

       try
       {
           utx = (UserTransaction) initialContext.lookup("javax.transaction.UserTransaction");
           //out.println("Creation of utx...OK   ");
       }
       catch (NamingException e) 
       {
           out.println("Cannot lookup UserTransaction: " + e);
           System.exit(2);
       }

		try
		{
           dishEntityFacade = (DishEntityFacadeRemote) initialContext.lookup("ecom.beans.dish.DishEntityFacade" + "_" + DishEntityFacadeRemote.class.getName() + "@Remote");
           //out.println("Creation of dishEntityFacade...OK   ");
       }
		catch (NamingException e) 
		{
           out.println("Cannot get dishEntityFacade : " + e);
           System.exit(2);
       }

    	

        
        out.println("<html>");
        out.println("<head>");
        out.println("<link type=\"text/css\" href=\"../ow_jonas.css\" rel=\"stylesheet\" id=\"stylesheet\">");
        out.println("<title>");
        out.println("Product Servlet</title>");
        out.println("</head>");
        out.println("<body style=\"background : white; color : black;\">");
        
        List<DishEntity> dishList = null;
        dishList = dishEntityFacade.getAllDishes();
        if (dishList != null && dishList.size() > 0) {

            out.println("-------------------------------");
            for (DishEntity dish : dishList) {
			
				if(dish.getDishName().length()>35){
				
					out.println("[" + dish.getDishId() + "] " + dish.getDishName() + "\t[" + dish.getPrice() + "] Euro");
				}
				else if(dish.getDishName().length()<=15){
					
					out.println("[" + dish.getDishId() + "] " + dish.getDishName() + "\t\t\t\t\t[" + dish.getPrice() + "] Euro");
				}
				else if(dish.getDishName().length()<=24 &&  dish.getDishName().length()>16){
					
					out.println("[" + dish.getDishId() + "] " + dish.getDishName() + "\t\t\t\t[" + dish.getPrice() + "] Euro");
				}
				else if(dish.getDishName().length()==25){
					
					out.println("[" + dish.getDishId() + "] " + dish.getDishName() + "\t\t\t[" + dish.getPrice() + "] Euro");
				}
				else
					out.println("[" + dish.getDishId() + "] " + dish.getDishName() + "\t\t\t\t[" + dish.getPrice() + "] Euro");
                
            }

        } else {
            out.println("Aucun plat dans le menu.");
        }
       


        
        
        out.println("</h1>");
        out.println("</body>");
        out.println("</html>");
    }
}
